package com.scalar.chiptrack.operations.inventoryps.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.inventoryps.dao.InventoryPSDao;
import com.scalar.chiptrack.operations.inventoryps.forms.InventoryPackingSlipSearchForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.utils.DateUtil;

public class InventoryPackingSlipSearchAction   extends ChipTrackAction implements SessionKeys
{
	public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
        HashMap<String, String> errors = new HashMap<String, String>();
        request.setAttribute( ERROR_MESSAGES, errors );
        
        InventoryPackingSlipSearchForm inventoryPSSearchForm = (InventoryPackingSlipSearchForm) form;
        
        ActionForward actionForward;
        String action = request.getParameter( "action" );
        
        try
        {
        	if( action == null || "load".equalsIgnoreCase( action ) )
        	{
            	request.setAttribute( "processTypeList", NewProductDAO.loadAllProcessesFromDB( null, false ) );
            	if( inventoryPSSearchForm.getSearchProcessType() != null && !"0".equalsIgnoreCase( inventoryPSSearchForm.getSearchProcessType() ) )
            	{
            		request.setAttribute( "partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( inventoryPSSearchForm.getSearchProcessType() ) );
            	}
            	else
            	{
            		request.setAttribute( "partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( "FG" ) );
            	}
            	
            	request.setAttribute( "locationList",    VendorDAO.getAllVendors() );
            	request.setAttribute( "toLocationList",  VendorDAO.getAllVendors() );
                
            	if( action == null )
            	{
	            	inventoryPSSearchForm.setSearchProcessStepId( 0 );
	            	inventoryPSSearchForm.setSearchLocationId( 0 );
	            	inventoryPSSearchForm.setSearchToVendorId( 0 );
	            	inventoryPSSearchForm.setSearchProcessType( "0" );
	            	inventoryPSSearchForm.setSearchFromDate( DateUtil.getPreviousDate( 365 ) );
	            	inventoryPSSearchForm.setSearchToDate( DateUtil.getCurrentDate() );
            	}
            	inventoryPSSearchForm.setPackingSlipList( null );
            	
        	}
        	else if( "search".equalsIgnoreCase( action ) )
        	{
            	request.setAttribute( "processTypeList", NewProductDAO.loadAllProcessesFromDB( null, false ) );
            	request.setAttribute( "partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( inventoryPSSearchForm.getSearchProcessType() ) );
            	request.setAttribute( "locationList",    VendorDAO.getAllVendors() );
            	request.setAttribute( "toLocationList",  VendorDAO.getAllVendors() );
            	
            	inventoryPSSearchForm.setPackingSlipList( InventoryPSDao.searchPackingSlip( inventoryPSSearchForm ) );
        	}
        	
        	actionForward = mapping.findForward("search");
        }
        catch (Exception e)
        {
            errors.put("Unable to load the Proecss Type in Inventory Packing Slip Screen", null);
            actionForward = mapping.findForward( "inventoryPS" );
        }
        return actionForward;
	}

}
